<?php

if (isset($_POST['action']) && $_POST['action'] == "register") {
	global $_IAESALT;

	
	$validated		= 	1;
	$salt 			= 	create_salt(5);
	$global_salt	= 	$_IAESALT;
	$email			=	'';
	$name			=	'';
	$username		=	'';
	$password1		=	'';
	$password2		=	'';

	$check = $user->checkRegistration($_POST['username'],  $_POST['email']);

	if (!empty($_POST['email']))
	{
		if($check['email'])
		{
				$smarty->assign('email_error', "This email adress has already been registered.");
				$validated = 0;
		}
		else 
		{
				$email = $_POST['email'];
		}
//		if (check_email_adress($email)) {
//			$smarty->assign('email_error', "The entered email adress is not in the correct format.<br>Please try again!.");
//			$validated = 0;
//
//		}
//

	}
	else
	{
		$smarty->assign('email_error', "Please insert an email adress.");
		$validated = 0;
	}

	if (!empty($_POST['username']))
	{
		if($check['user'])
		{
				$smarty->assign('username_error', "This user name has already been registered.");
				$validated = 0;
		}
		else 
		{
				$username = $_POST['username'];
		}
		
		
	}
	else
	{
		$smarty->assign('username_error', "Please insert an username.");
		$validated = 0;
	}

	
	
	if (!empty($_POST['name']))
	{
		$name = $_POST['name'];


	}
	else
	{
		$smarty->assign('name_error', "Please insert an name.");
		$validated = 0;
	}

	if (!empty($_POST['password1']))
	{
		$password1 = $_POST['password1'];

		if (!empty($_POST['password2']))
		{
			$password2 = $_POST['password2'];

			if ($password1 == $password2) {
				
				$password = $password1;
				
			} else {
				
				$smarty->assign('password1_error', "The inserted passwords are not the same,<br>please try again.");
				$validated = 0;
				
			}
			
		}
		else
		{
			$smarty->assign('password2_error', "Please insert an confirm password.");
			$validated = 0;
		}
		
	}
	else
	{
		$smarty->assign('password1_error', "Please insert an password.");
		$validated = 0;
	}

	

	if ($validated == 0) {

		$smarty->assign('name', $name);
		$smarty->assign('password1', '');
		$smarty->assign('password2', '');
		$smarty->assign('email', $email);
		$smarty->assign('username', $username);

		$smarty->display('registration.tpl');

	} else {

		$class_display = $_POST['class_display'];
		
		$user->save(0, $name, $email, $password1, $username, 1, $salt, $class_display);

		
		$smarty->assign('message', "Registration was succesfull, click on Login at the right to login!");
		$smarty->display('registration.tpl');
		
	}


} else {

	$smarty->display('registration.tpl');

}


?>  